import { Message } from '@alifd/next';
import scheduleService from '@/services/schedule';

export default {
  state: {
    list: [],
  },
  reducers: {
    update: (prevState, payload) => {
      return { ...prevState, ...payload };
    },
  },
  effects: (dispatch) => ({
    getList: async (params) => {
      const data = await scheduleService.list(params);
      if (data.code === 0) {
        dispatch.schedule.update({ list: data.page });
      } else {
        Message.error(data.msg);
      }
    },
    postCreate: async (data) => {
      const res = await scheduleService.add(data);
      if (res.code === 0) {
        Message.success({
          content: '新增成功！',
          hasMask: true,
        });
      } else {
        Message.error({
          content: res.msg,
          hasMask: true,
        });
      }
    },
    postUpdate: async (data) => {
      const res = await scheduleService.update(data);
      if (res.code === 0) {
        Message.success({
          content: '编辑成功！',
          hasMask: true,
        });
      } else {
        Message.error({
          content: res.msg,
          hasMask: true,
        });
      }
    },
    postDelete: async (ids) => {
      const res = await scheduleService.delete(ids);
      if (res.code === 0) {
        Message.success({
          content: '删除成功！',
          hasMask: true,
        });
      } else {
        Message.error({
          content: res.msg,
          hasMask: true,
        });
      }
    },
    postPause: async (ids) => {
      const res = await scheduleService.pause(ids);
      if (res.code === 0) {
        Message.success({
          content: '暂停成功！',
          hasMask: true,
        });
      } else {
        Message.error({
          content: res.msg,
          hasMask: true,
        });
      }
    },
    postResume: async (ids) => {
      const res = await scheduleService.resume(ids);
      if (res.code === 0) {
        Message.success({
          content: '恢复成功！',
          hasMask: true,
        });
      } else {
        Message.error({
          content: res.msg,
          hasMask: true,
        });
      }
    },
    postRun: async (ids) => {
      const res = await scheduleService.run(ids);
      if (res.code === 0) {
        Message.success({
          content: '执行成功！',
          hasMask: true,
        });
      } else {
        Message.error({
          content: res.msg,
          hasMask: true,
        });
      }
    },
  }),
};
